import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 线索飞书 (单线索)监听 群机器人
 *
 * @author wangsong
 * @date 2023/04/10
 */
@Slf4j
public class WeiXiMsgApi {


    /**
     * 测试环境
     */
    private static final String TEST_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=28fe9173-380b-4a13-a912-996e99a3ae59";


    /**
     * 发送线索接口消息
     *
     * @param content 内容
     * @return boolean
     */
    public static boolean sendMsg(String content,String phone) {
        Map<String, Object> contentMap = new HashMap<>(4);
        contentMap.put("content", content);
        if (phone != null && !"".equals(phone)) {
            contentMap.put("mentioned_mobile_list", Arrays.asList(phone.split(",")));
        }
        sendMsg(contentMap);
        return true;
    }


    private static void sendMsg(Map<String, Object> contentMap) {

        // 拼接消息体
        Map<String, Object> paramMap = new HashMap<>(4);
        paramMap.put("msgtype", "text");
        paramMap.put("text", contentMap);
        // 请求
        String body = HttpRequest.post(TEST_URL)
                .header("Content-Type", "application/json;charset=utf-8")
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("发送微信群消息: " + body);

    }

    public static void main(String[] args) {
        try {
            int a = 1/0;
        }catch (Exception e){
            String content = "【异常监控】" +
                    "\n系统异常了, 请及时检查测试信息\n " +
                    e.getMessage();
            WeiXiMsgApi.sendMsg(content,"xxxxxxxxxx");
        }
    }
}
